import logging
import os


def setup_logger(name: str, log_file: str, level=logging.INFO) -> logging.Logger:
    os.makedirs(os.path.dirname(log_file), exist_ok=True)

    handler = logging.FileHandler(log_file, encoding="utf-8")
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] [%(threadName)s] [%(filename)s:%(lineno)d]: %(message)s'
    )
    handler.setFormatter(formatter)
    logger = logging.getLogger(name)
    logger.setLevel(level)
    # 避免重复添加 Handler
    if not logger.handlers:
        logger.addHandler(handler)

    return logger
